<template>
  <div class="register-area" data-info="自助挂号" v-loading="loading">
    <el-button class="next" @click="handleNext" type="primary">
      下一步{{ stepInfo }}
    </el-button>

    <div class="step-comps-area" data-info="具体的业务">
      <component
          :is="curCompName"
          @handlePatientInfoSure="handlePatientInfoSure"/>
    </div>
    <!--底部操作-->
    <PatientNumberOperate
        :key="widget"
        @handleBack="handlePre"/>
  </div>
</template>

<script>
import OPType from "@sc/DRCbiz/comps/OPType/index.vue";
import CardType from "@sc/DRCbiz/comps/cardType/index.vue";
import MedInsCode from "@sc/DRCbiz/comps/medInsCode/index.vue";
import PatientInfo from "@sc/DRCbiz/comps/patientInfo/index.vue";
import PatientDept from "@sc/DRCbiz/comps/patientDept/index.vue";
import PatientNumberType from "@sc/DRCbiz/comps/hospAreaInfo/index.vue";
import PatientNumberInfo from "@sc/DRCbiz/comps/patientNumberInfo/index.vue";
import PatientNumberDate from "@sc/DRCbiz/comps/patientNumberDate/index.vue";
import RegisterInfo from '@sc/DRCbiz/comps/registerInfo';
import DoctorSchedul from "@sc/DRCbiz/comps/doctorSchedul/index.vue";
import PatientNumberOperate from "@sc/DRCbiz/comps/patientNumberOperate/index.vue";
import PatientNumberTimeRang from "@sc/DRCbiz/comps/patientNumberTimeRang/index.vue";
import PatientNumberRezResult from "@sc/DRCbiz/comps/patientNumberRezResult/index.vue";
import {buildParentApi} from "@/utils/designer/util";
import {v4 as uuid} from "uuid";
import lodash from "lodash";

let buildParent = buildParentApi();
export default {
  name: 'Register',

  componentName: "Register",

  components: {
    CardType,
    MedInsCode,
    PatientInfo,
    OPType,
    PatientDept,
    PatientNumberType,
    PatientNumberDate,
    DoctorSchedul,
    PatientNumberTimeRang,
    PatientNumberInfo,
    RegisterInfo,
    PatientNumberRezResult,
    PatientNumberOperate,
  },

  data() {
    return {
      widget: uuid(),
      curCompName: 'CardType',
      // 普通-->科室-->号源信息--> 结果
      // 专家-->科室-->医生排班-->号源信息--> 结果
      compsOptions: {
        ordinary: {
          1: 'CardType',
          2: 'MedInsCode',
          3: 'PatientInfo',
          4: 'OPType',
          5: 'PatientDept',
          6: 'RegisterInfo',
          7: 'PatientNumberRezResult'
        },
        expert: {
          1: 'CardType',
          2: 'MedInsCode',
          3: 'PatientInfo',
          4: 'OPType',
          5: 'PatientDept',
          6: 'DoctorSchedul',
          7: 'RegisterInfo',
          8: 'PatientNumberRezResult',
        },
      },
    }
  },

  computed: {
    shownOperate() {
      return this.stepInfo !== 1;
    },

    // 步骤
    stepInfo() {
      return this.$store.getters['DRCbiz/getStepsInfo']
    },

    //获取Loading状态
    loading() {
      return this.$store.getters['layout/getLoading']
    },


    //选中的卡片信息
    cardTypeInfo() {
      return this.$store.getters['DRCbiz/getCardTypeInfo']
    },

    //选中的卡片信息
    OPTypeInfo() {
      return this.$store.getters['DRCbiz/getOPTypeInfo']
    },


  },

  mounted() {
  },

  methods: {
    // 下一步
    async handleNext() {
      let stepInfo = parseInt(this.stepInfo);
      this.widget = uuid();
      if (stepInfo === 7) {
        await this.$store.dispatch('DRCbiz/setStepsInfo', 1)
      } else {
        await this.$store.dispatch('DRCbiz/setStepsInfo', ++stepInfo)
      }
    },

    // 返回上一层&首页
    async handlePre() {
      this.widget = uuid();
      let stepInfo = parseInt(this.stepInfo)
      if (stepInfo === 1) {
        await this.$router.push({
          path: `/apptsReg/2`
        });
      } else {
        await this.$store.dispatch('DRCbiz/setStepsInfo', --stepInfo)
      }
      switch (parseInt(this.stepInfo)) {
        case 2:
          let cardTypeInfo = (this.cardTypeInfo);
          Reflect.set(cardTypeInfo, 'cardNo', null);
          await this.$store.dispatch('DRCbiz/setCardTypeInfo', cardTypeInfo);
          buildParent.executeParentMethod('openScan');
          break
      }
    },

    // 患者信息-->获取门诊类型
    handlePatientInfoSure() {
      let stepInfo = parseInt(this.stepInfo);
      this.$store.dispatch('DRCbiz/setStepsInfo', ++stepInfo)
    }
  },

  watch: {
    // 步距
    stepInfo: {
      handler: function (val) {
        const self = this;
        self.widget = uuid();
        let OPTypeInfo = (self.OPTypeInfo);
        let cardTypeInfo = (self.cardTypeInfo);
        if (val === 2) {
          // 电子医保特殊处理
          if (parseInt(cardTypeInfo?.id) === 29) {
            buildParent.executeParentMethod('MICode');
          } else {
            buildParent.executeParentMethod('openScan');
          }
        } else {
          buildParent.executeParentMethod('closeScan');
        }

        // 返回选择专家类型
        if(val === 5){
          this.$store.dispatch('DRCbiz/setAutoPreAppointReg',false);
        }

        self.$store.dispatch('DRCbiz/setCTDTimeInfo', 90000);
        if (parseInt(OPTypeInfo.id) === 1) {
          self.curCompName = Reflect.get(self.compsOptions['ordinary'], val);
        } else {
          self.curCompName = Reflect.get(self.compsOptions['expert'], val);
        }
      },
      deep: true,
      immediate: true,
    },
  }
}

</script>

<style scoped lang="scss">
@import "./assets/style";
</style>